<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>嵌入式syslog编译与使用 | blog</title>
  <meta name="description" content="busybox下的syslogd不能同时监控多个log，所以需要自己编译一个 syslog该工具在inetutils工具包内首先下载安装包，然后解压，编译1234wget ftp://ftp.gnu.org/gnu/inetutils/inetutils-1.5.tar.gztar xvf inetutils-1.5.tar.gz &amp;amp;&amp;amp; cd inetutils-1.5CC=arm">
<meta property="og:type" content="article">
<meta property="og:title" content="嵌入式syslog编译与使用">
<meta property="og:url" content="http://mijiku.com.cn/blog/2019/04/30/嵌入式syslog编译与使用/index.html">
<meta property="og:site_name" content="privatearea">
<meta property="og:description" content="busybox下的syslogd不能同时监控多个log，所以需要自己编译一个 syslog该工具在inetutils工具包内首先下载安装包，然后解压，编译1234wget ftp://ftp.gnu.org/gnu/inetutils/inetutils-1.5.tar.gztar xvf inetutils-1.5.tar.gz &amp;amp;&amp;amp; cd inetutils-1.5CC=arm">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-30T06:59:12.006Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="嵌入式syslog编译与使用">
<meta name="twitter:description" content="busybox下的syslogd不能同时监控多个log，所以需要自己编译一个 syslog该工具在inetutils工具包内首先下载安装包，然后解压，编译1234wget ftp://ftp.gnu.org/gnu/inetutils/inetutils-1.5.tar.gztar xvf inetutils-1.5.tar.gz &amp;amp;&amp;amp; cd inetutils-1.5CC=arm">
  <!-- Canonical links -->
  <link rel="canonical" href="http://mijiku.com.cn/blog/2019/04/30/嵌入式syslog编译与使用/index.html">
  
    <link rel="alternate" href="/atom.xml" title="privatearea" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  
  
  
    <link rel="stylesheet" href="/css//gitalk.min.css">
  
</head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/kof98765" target="_blank">
          <img class="img-circle img-rotate" src="/blog/images/head.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">陶大少</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">野火烧不尽，春风吹又生</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Dongguan, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索">
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech>
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav ">
        
        
        <li class="menu-item menu-item-home">
          <a href="/blog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/kof98765" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://weibo.com/98765kof" target="_blank" title="Weibo" data-toggle="tooltip" data-placement="top"><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      

    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/61850/">61850</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/git/">git</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/linux/">linux</a><span class="tag-list-count">6</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/blog/tags/61850/" style="font-size: 13.5px;">61850</a> <a href="/blog/tags/git/" style="font-size: 13px;">git</a> <a href="/blog/tags/linux/" style="font-size: 14px;">linux</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2021/01/">一月 2021</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/12/">十二月 2020</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/11/">十一月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/05/">五月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/04/">四月 2019</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2017/03/">三月 2017</a><span class="archive-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/blog/2021/01/14/IEC61850建模说明/" class="title">IEC61850建模说明</a>
              </p>
              <p class="item-date">
                <time datetime="2021-01-14T03:12:36.209Z" itemprop="datePublished">2021-01-14</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/blog/2021/01/14/61850服务构建流程/" class="title">61850构建流程</a>
              </p>
              <p class="item-date">
                <time datetime="2021-01-14T01:58:48.549Z" itemprop="datePublished">2021-01-14</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/blog/2020/12/31/蓝牙profile简介/" class="title">蓝牙profile简介</a>
              </p>
              <p class="item-date">
                <time datetime="2020-12-31T08:41:11.458Z" itemprop="datePublished">2020-12-31</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/blog/2020/12/31/蓝牙各版本之前的区别/" class="title">蓝牙各版本之间的区别</a>
              </p>
              <p class="item-date">
                <time datetime="2020-12-31T08:29:31.819Z" itemprop="datePublished">2020-12-31</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/blog/2020/12/31/linux下蓝牙服务端和客户端编写/" class="title">linux 蓝牙服务端和客户端编写</a>
              </p>
              <p class="item-date">
                <time datetime="2020-12-31T08:24:13.593Z" itemprop="datePublished">2020-12-31</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-嵌入式syslog编译与使用" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      嵌入式syslog编译与使用
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/blog/2019/04/30/嵌入式syslog编译与使用/" class="article-date">
	  <time datetime="2019-04-30T06:37:07.000Z" itemprop="datePublished">2019-04-30</time>
	</a>
</span>
        
        

        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/blog/2019/04/30/嵌入式syslog编译与使用/#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <p>busybox下的syslogd不能同时监控多个log，所以需要自己编译一个</p>
<h2 id="syslog"><a href="#syslog" class="headerlink" title="syslog"></a>syslog</h2><p>该工具在inetutils工具包内<br>首先下载安装包，然后解压，编译<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wget ftp://ftp.gnu.org/gnu/inetutils/inetutils-1.5.tar.gz</span><br><span class="line">tar xvf inetutils-1.5.tar.gz &amp;&amp; cd inetutils-1.5</span><br><span class="line">CC=arm-none-linux-gnueabi-gcc LDFLAGS=-static ./configure --disable-clients --disable-ipv6 --disable-ncurses --host=arm --prefix=/opt/inetutils</span><br><span class="line">make</span><br></pre></td></tr></table></figure></p>
<p>之后可在src/下找到syslogd，将之复制到开发板的/sbin目录内</p>
<p>但syslog不支持多线程打印，当两条线程同时使用时，需要自行加锁<br>rsyslog是syslog的升级版，支持多线程<br>编译步骤一样，但需要多安装一些库,这里就不多说了</p>
<p>syslog可以通过配置文件控制日志的输出，但不能指定日志的大小<br>所以需要用到logrotate命令</p>
<h2 id="logrotate"><a href="#logrotate" class="headerlink" title="logrotate"></a>logrotate</h2><p>logrotate是linux中日志管理的重要工具，它可以自动对日志进行截断（或轮循）、压缩以及删除旧的日志文件。在发行版的桌面或者服务器linux系统中这个工具安装一般都是比较容易，或者默认已经自带，但是嵌入式系统一般需要通过源码来自己构建。</p>
<p>先安装编译依赖工具<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt-get install libtool-bin</span><br><span class="line">apt-get install autopoint</span><br></pre></td></tr></table></figure></p>
<h2 id="构建POPT库"><a href="#构建POPT库" class="headerlink" title="构建POPT库"></a>构建POPT库</h2><p>由于logrotate依赖于POPT库，所以要生成logrotate需要先构建POPT库，然后再利用POPT库构建logrotate。<br>下载最新的master分支源码包,并编译<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/devzero2000/POPT</span><br></pre></td></tr></table></figure></p>
<p>由于源码是基于autotools编译的，所以需要安装以下两个依赖工具<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt-get install libtool </span><br><span class="line">apt-get install autoconf</span><br></pre></td></tr></table></figure></p>
<p>第一次编译需要先生成configure,执行以下命令<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./autogen.sh</span><br></pre></td></tr></table></figure></p>
<p>之后就可以使用configure这个程序来生成Makefile了，比如下面就是生成 arm版的命令<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">./configure --prefix=/opt/POPT --host=arm-linux</span><br><span class="line">make</span><br><span class="line">make install</span><br></pre></td></tr></table></figure></p>
<p>构建好后会在/opt/POPT 生成“include  lib  share” 三个目录，其中lib目录有logrotate编译和运行需要的库文件，include目录有我们编译logrotate需要的头文件。</p>
<h2 id="构建logrotate"><a href="#构建logrotate" class="headerlink" title="构建logrotate"></a>构建logrotate</h2><p>下载最新的源码logrotate,并编译<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/logrotate/logrotate </span><br><span class="line">./autogen.sh</span><br><span class="line">#./configure --host=arm-linux --prefix=/opt/logrotate LDFLAGS=-L/opt/POPT/lib CPPFLAGS=-I/opt/POPT/include</span><br><span class="line">make </span><br><span class="line">make install</span><br></pre></td></tr></table></figure></p>
<p>其中LDFLAGS指定logrotate编译依赖库lpopt路径；而CPPFLAGS指定logrotate编译依赖头文件popt.h路径。<br>构建好后会在/opt/logrotate目录生成sbin/logrotate文件。</p>
<p>将/opt/POPT/lib/libpopt.so.0.0.0 复制到开发板的/lib下，并将名称改为libpopt.so.0<br>将/opt/logrotate/sbin/logrotate 复制到开发板的/sbin目录</p>
<p>编写一个配置文件,存放到开发板的/etc目录下，名称为logrotate.cfg<br>内容如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">/var/log/testlog.log &#123;</span><br><span class="line">     size=100k</span><br><span class="line">     rotate 2</span><br><span class="line">     postrotate</span><br><span class="line">                    /usr/bin/killall -HUP syslogd</span><br><span class="line">     endscript</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>脚本的功能为监控/var/log/testlog.log,当该文件大于100k时，备份，最多备份2次，每次备份时，会kill掉syslogd，不过syslogd是会自动重启的不用担心</p>
<p>之后还需要启动一个定时任务来周期性调用logrotate命令</p>
<p>首先在开发板上创建/etc/cron目录<br>开机脚本写上<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">crond -c /etc/cron</span><br><span class="line">crontab -c /etc/cron /etc/cron/logrotate</span><br></pre></td></tr></table></figure></p>
<p>开机脚本每个系统都不太一样，就不说了<br>之后编写一个cron的配置文件放在/etc/cron目录下，文件名为logrotate<br>内容为:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">* * * * * logrotate /etc/logrotate.cfg</span><br></pre></td></tr></table></figure></p>
<p>内容的意思是每分钟执行一次logrotate，这样就可以自动备份转存日志了</p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://mijiku.com.cn/blog/2019/04/30/嵌入式syslog编译与使用/" title="嵌入式syslog编译与使用" target="_blank" rel="external">http://mijiku.com.cn/blog/2019/04/30/嵌入式syslog编译与使用/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/kof98765" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/blog/images/head.jpg" class="img-rounded w-full" alt>
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/kof98765" target="_blank"><span class="text-dark">陶大少</span><small class="ml-1x">野火烧不尽，春风吹又生</small></a></h3>
        <div>路漫漫其修远兮，吾将上下而求索</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
           
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/blog/2019/04/30/大量time-wait的处理方法/" title="大量time wait的处理方法"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/blog/2019/04/29/使用git上传并自动部署服务端代码/" title="使用git上传并自动部署服务端代码"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/kof98765" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://weibo.com/98765kof" target="_blank" title="Weibo" data-toggle="tooltip" data-placement="top"><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="/js/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/blog/js/plugin.min.js"></script>
<script src="/blog/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/blog/',
        CONTENT_URL: '/blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/blog/js/insight.js"></script>





   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   
    
  <!-- <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"> -->
  <script src="/js/gitalk.min.js"></script>
  <script src="/js/md5.min.js"></script>
  <script type="text/javascript">
  var gitalk = new Gitalk({
    clientID: '18d1dbf08be917781fd0',
    clientSecret: 'f87d036360e8a7c25e6810211f149b4b8955b8f3',
    repo: 'blog_by_talk',
    owner: 'kof98765',
    admin: ['kof98765'],
    id: md5(location.pathname),
    distractionFreeMode: true
  })
  gitalk.render('comments')
  </script>

      








</body>
</html>